﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EAMF.Content.NPCs.Boss
{
    [AutoloadBossHead] //定义为BOSS
    public class RedEye : ModNPC
    {
        public Vector2 vector2;
        public ref float AI_State => ref NPC.ai[0];
        public ref float AI_Timer => ref NPC.ai[1];
        public int prame = 0;
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 4;
        }
        public override void SetDefaults()
        {
            NPC.width = 32;
            NPC.height = 128 / 4;
            NPC.damage = 60;
            NPC.lifeMax = 5000; //最大生命值
            NPC.defense = 15;
            NPC.knockBackResist = 0.1f; //被击退能力
            NPC.HitSound = SoundID.NPCHit9;
            NPC.DeathSound = SoundID.NPCDeath9;
            NPC.value = Item.buyPrice(0, 20, 0, 0);
            NPC.noGravity = true; //是否不受重力影响
            NPC.noTileCollide = true;//是否可以穿墙
            NPC.boss = true; //设置成boss
            AI_State = 0;
        }
        public override void FindFrame(int frameHeight)
        {
            int startFrame = 0;
            int finalFrame = 3;

            int frameSpeed = 5;
            NPC.frameCounter += 0.5f;
            NPC.frameCounter += NPC.velocity.Length() / 10f; // Make the counter go faster with more movement speed
            if (NPC.frameCounter > frameSpeed)
            {
                NPC.frameCounter = 0;
                NPC.frame.Y += frameHeight;

                if (NPC.frame.Y > finalFrame * frameHeight)
                {
                    NPC.frame.Y = startFrame * frameHeight;
                }
            }
        }
        public override void AI()
        {
            AI_Timer++;
            NPC.TargetClosest(true);//找到最近的玩家
            Player p = Main.player[NPC.target];//筛选出这个玩家

            //velocity代表速度(向量),Center代表位置(向量),position代表坐标
            //当这个物体有速度时它会每帧触发这个速度到下一个位置
            if (AI_Timer % 100 == 1)//每100个帧锁定玩家的方向
            {
                vector2 = p.Center - NPC.Center;
            }
            if (AI_Timer <= 200)//这里将执行两百下,由于帧很快所以速度别太快了，系统只记的帧
            {
                if (AI_Timer % 20 == 1)//每十个帧向玩家冲撞
                {
                    NPC.velocity = 20 * vector2.SafeNormalize(Vector2.Zero);//SafeNormalize(Vector2.Zero)可以修正向量的长度防止速度过快                                                   //向目标发起冲撞
                }
            }
            if (AI_Timer > 200 && AI_Timer < 400)
            {
                //原地不懂开始回血
                NPC.velocity = Vector2.Zero;//速度置零,Vector2.Zero代表一个值为0的向量
                if (AI_Timer % 5 == 0)//将回血次数除以五
                {
                    NPC.life += 5;
                }
            }
            if (AI_Timer == 400)
            {
                //瞬移到玩家的正上方
                NPC.position = p.position + new Vector2(0, -200);
                AI_Timer = 0;//置零重新开始
            }
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (!Main.dayTime)
            {
                return 0.01f;
            }
            return 0;
        }
    }
}
